package com.housing.service.impl;

import com.zmetaport.common.core.utils.MapstructUtils;
import com.zmetaport.common.core.utils.StringUtils;
import com.zmetaport.common.mybatis.core.page.TableDataInfo;
import com.zmetaport.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.housing.domain.bo.RentalConfigBo;
import com.housing.domain.vo.RentalConfigVo;
import com.housing.domain.RentalConfig;
import com.housing.mapper.RentalConfigMapper;
import com.housing.service.IRentalConfigService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author chensm
 * @date 2024-12-15
 */
@RequiredArgsConstructor
@Service
public class RentalConfigServiceImpl implements IRentalConfigService {

    private final RentalConfigMapper baseMapper;

    /**
     * 查询【请填写功能名称】
     */
    @Override
    public RentalConfigVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public TableDataInfo<RentalConfigVo> queryPageList(RentalConfigBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<RentalConfig> lqw = buildQueryWrapper(bo);
        Page<RentalConfigVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public List<RentalConfigVo> queryList(RentalConfigBo bo) {
        LambdaQueryWrapper<RentalConfig> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<RentalConfig> buildQueryWrapper(RentalConfigBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<RentalConfig> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getType() != null, RentalConfig::getType, bo.getType());
        lqw.eq(bo.getSourceId() != null, RentalConfig::getSourceId, bo.getSourceId());
        lqw.eq(bo.getRent() != null, RentalConfig::getRent, bo.getRent());
        lqw.eq(bo.getCurrency() != null, RentalConfig::getCurrency, bo.getCurrency());
        lqw.eq(bo.getRentType() != null, RentalConfig::getRentType, bo.getRentType());
        lqw.eq(bo.getDeposit() != null, RentalConfig::getDeposit, bo.getDeposit());
        return lqw;
    }

    /**
     * 新增【请填写功能名称】
     */
    @Override
    public Boolean insertByBo(RentalConfigBo bo) {
        RentalConfig add = MapstructUtils.convert(bo, RentalConfig.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改【请填写功能名称】
     */
    @Override
    public Boolean updateByBo(RentalConfigBo bo) {
        RentalConfig update = MapstructUtils.convert(bo, RentalConfig.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(RentalConfig entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除【请填写功能名称】
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
